package l2hc.gameserver.handler.admincommands.impl;

import l2hc.gameserver.handler.admincommands.IAdminCommandHandler;
import l2hc.gameserver.instancemanager.ReflectionManager;
import l2hc.gameserver.instancemanager.SoDManager;
import l2hc.gameserver.model.Player;
import l2hc.gameserver.model.entity.Reflection;
import l2hc.gameserver.network.serverpackets.NpcHtmlMessage;
import l2hc.gameserver.scripts.Functions;

public class AdminInstance implements IAdminCommandHandler
{
	private static enum Commands
	{
		admin_instance,
		admin_instance_id,
		admin_collapse,
		admin_reset_reuse,
		admin_reset_reuse_all,
		admin_set_reuse,
		admin_addtiatkill
	}

	@SuppressWarnings("rawtypes")
	@Override
	public boolean useAdminCommand(Enum comm, String[] wordList, String fullString, Player activeChar)
	{
		Commands command = (Commands) comm;

		if(!activeChar.getPlayerAccess().CanTeleport)
			return false;

		switch(command)
		{
			case admin_instance:
				listOfInstances(activeChar);
				break;
			case admin_instance_id:
				if(wordList.length > 1)
					listOfCharsForInstance(activeChar, wordList[1]);
				break;
			case admin_collapse:
				if(!activeChar.getReflection().isDefault())
					activeChar.getReflection().collapse();
				else
					activeChar.sendMessage("Отражение по умолчанию не может разрушиться!");
				break;
			case admin_reset_reuse:
				if(wordList.length > 1 && activeChar.getTarget() != null && activeChar.getTarget().isPlayer())
				{
					Player p = activeChar.getTarget().getPlayer();
					p.removeInstanceReuse(Integer.parseInt(wordList[1]));
					Functions.sendDebugMessage(activeChar, "Инстанс был повторно удален");
				}
				break;
			case admin_reset_reuse_all:
				if(activeChar.getTarget() != null && activeChar.getTarget().isPlayer())
				{
					Player p = activeChar.getTarget().getPlayer();
					p.removeAllInstanceReuses();
					Functions.sendDebugMessage(activeChar, "Все инстансы были повторно удалены");
				}
				break;
			case admin_set_reuse:
				if(activeChar.getReflection() != null)
					activeChar.getReflection().setReenterTime(System.currentTimeMillis());
				break;
			case admin_addtiatkill:
				SoDManager.addTiatKill();
				break;
		}

		return true;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Enum[] getAdminCommandEnum()
	{
		return Commands.values();
	}

	private void listOfInstances(Player activeChar)
	{
		NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
		StringBuffer replyMSG = new StringBuffer("<html><title>Меню инстансов</title><body>");
		replyMSG.append("<table width=260><tr>");
		replyMSG.append("<td width=40><button value=\"Главная\" action=\"bypass -h admin_admin\" width=60 height=20 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
		replyMSG.append("<td width=180><center>Список инстансов</center></td>");
		replyMSG.append("<td width=40><button value=\"Назад\" action=\"bypass -h admin_admin\" width=44 height=20 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
		replyMSG.append("</tr></table><br><br>");

		for(Reflection reflection : ReflectionManager.getInstance().getAll())
		{
			if(reflection == null || reflection.isDefault() || reflection.isCollapseStarted())
				continue;
			int countPlayers = 0;
			if(reflection.getPlayers() != null)
				countPlayers = reflection.getPlayers().size();
			replyMSG.append("<a action=\"bypass -h admin_instance_id ").append(reflection.getId()).append(" \">").append(reflection.getName()).append("(").append(countPlayers).append(" players). ID: ").append(reflection.getId()).append("</a><br>");
		}

		replyMSG.append("<button value=\"Обновить\" action=\"bypass -h admin_instance\" width=55 height=20 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\">");
		replyMSG.append("</body></html>");

		adminReply.setHtml(replyMSG.toString());
		activeChar.sendPacket(adminReply);
	}

	private void listOfCharsForInstance(Player activeChar, String sid)
	{
		Reflection reflection = ReflectionManager.getInstance().get(Integer.parseInt(sid));

		NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
		StringBuffer replyMSG = new StringBuffer("<html><title>Меню инстансов</title><body><br>");
		if(reflection != null)
		{
			replyMSG.append("<table width=260><tr>");
			replyMSG.append("<td width=40><button value=\"Главная\" action=\"bypass -h admin_admin\" width=60 height=20 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
			replyMSG.append("<td width=180><center>Список игроков в ").append(reflection.getName()).append("</center></td>");
			replyMSG.append("<td width=40><button value=\"Назад\" action=\"bypass -h admin_instance\" width=45 height=20 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
			replyMSG.append("</tr></table><br><br>");

			for(Player player : reflection.getPlayers())
				replyMSG.append("<a action=\"bypass -h admin_teleportto ").append(player.getName()).append(" \">").append(player.getName()).append("</a><br>");
		}
		else
		{
			replyMSG.append("Инстанс не активен.<br>");
			replyMSG.append("<a action=\"bypass -h admin_instance\">К списку.</a><br>");
		}

		replyMSG.append("</body></html>");

		adminReply.setHtml(replyMSG.toString());
		activeChar.sendPacket(adminReply);
	}
}